package com.kyrie.lib_sys.application.categoryApplys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kyrie.lib_sys.common.entity.PageResult;
import com.kyrie.lib_sys.common.pojo.dto.CategoryDTO;
import com.kyrie.lib_sys.common.pojo.dto.CategoryPageQueryDTO;
import com.kyrie.lib_sys.common.pojo.entity.Category;
import com.kyrie.lib_sys.application.categoryApplys.mapper.CategoryMapper;
import com.kyrie.lib_sys.application.categoryApplys.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类service类
 *
 * @author heefM
 * @date 2025-08-09
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加分类
     *
     * @param categoryDTO
     */
    @Override
    public void insert(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.insert(category);
    }

    /**
     * 查询分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public Category queryCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category = categoryMapper.queryByCondition(category);
        return category;
    }

    /**
     * 根据ID查询分类
     *
     * @param id
     * @return
     */
    @Override
    public CategoryDTO queryCategoryById(Long id) {
        Category category = categoryMapper.queryById(id);
        if (category != null) {
            CategoryDTO categoryDTO = new CategoryDTO();
            BeanUtils.copyProperties(category, categoryDTO);
            return categoryDTO;
        }
        return null;
    }

    /**
     * 查询分类列表
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryList(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPageNum(), categoryPageQueryDTO.getPageSize());

        Page<Category> page = categoryMapper.queryList(categoryPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 分页查询分类
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPageNum(), categoryPageQueryDTO.getPageSize());

        Page<Category> page = categoryMapper.queryList(categoryPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 条件查询分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public CategoryDTO queryByCondition(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        Category result = categoryMapper.queryByCondition(category);
        if (result != null) {
            CategoryDTO resultDTO = new CategoryDTO();
            BeanUtils.copyProperties(result, resultDTO);
            return resultDTO;
        }
        return null;
    }

    /**
     * 更新分类
     *
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.update(category);
    }

    /**
     * 删除分类
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }

    /**
     * 批量删除分类
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        categoryMapper.deleteBatch(ids);
    }

    /**
     * 批量插入分类
     *
     * @param categoryDTOS
     */
    @Override
    public void insertBatch(List<CategoryDTO> categoryDTOS) {
        ArrayList<Category> categoryList = new ArrayList<>();
        for (CategoryDTO categoryDTO : categoryDTOS) {
            if (categoryDTO != null) {
                Category category = new Category();
                BeanUtils.copyProperties(categoryDTO, category);
                categoryList.add(category);
            }
        }
        categoryMapper.insertBatch(categoryList);
    }
}
